import { apiRequest } from './api';

export interface DataStatus {
  totalTasks: number;
  completedTasks: number;
  tasksLast30Days: number;
  tasksLast60Days: number;
  categoriesWithData: number;
  prioritiesWithData: number;
  readiness: {
    workPatterns: boolean;
    suggestions: boolean;
    procrastination: boolean;
    predictions: boolean;
    overall: boolean;
  };
}

export interface WorkPatterns {
  hourlyCreation: Array<{ hour: number; count: number }>;
  hourlyCompletion: Array<{ hour: number; count: number }>;
  weekdayEfficiency: Array<{
    dayType: 'weekday' | 'weekend';
    completedCount: number;
    avgCompletionDays: string | null;
  }>;
  peakHours: Array<{
    hour: number;
    completedCount: number;
    avgCompletionDays: string | null;
  }>;
}

export interface ProcrastinationAnalysis {
  procrastinationByCategory: Array<{
    priority: string;
    category: string;
    totalCount: number;
    completedCount: number;
    completionRate: number;
    avgCompletionDays: string;
  }>;
  creationPatterns: Array<{
    dayOfWeek: number;
    hourOfDay: number;
    taskCount: number;
    completedCount: number;
  }>;
}

export interface ProductivitySuggestion {
  type: 'completion_rate' | 'procrastination' | 'category_focus' | 'task_load';
  priority: 'high' | 'medium' | 'low';
  title: string;
  description: string;
  actionable: boolean;
}

export interface ProductivitySuggestions {
  suggestions: ProductivitySuggestion[];
  stats: {
    totalTasks: number;
    completedTasks: number;
    completionRate: string;
    avgDelayDays: string;
  };
}

export interface TaskPrediction {
  taskId: string;
  title: string;
  priority: string;
  category: string;
  predictedDays: number | null;
  predictedCompletionDate: string | null;
  confidence: number;
  isLikelyOverdue: boolean;
  dueDate: string | null;
  note?: string;
}

export interface TaskPredictions {
  predictions: TaskPrediction[];
  model: Array<{
    priority: string;
    category: string;
    avgCompletionDays: string;
    sampleSize: number;
    confidence: number;
  }>;
}

class InsightsAPI {
  // 获取数据状态
  async getDataStatus(): Promise<DataStatus> {
    const response = await apiRequest('/insights/data-status', {
      method: 'GET'
    });
    return (response as any).data;
  }
  // 获取工作模式分析
  async getWorkPatterns(): Promise<WorkPatterns> {
    const response = await apiRequest('/insights/work-patterns', {
      method: 'GET'
    });
    return (response as any).data;
  }

  // 获取拖延行为分析
  async getProcrastinationAnalysis(): Promise<ProcrastinationAnalysis> {
    const response = await apiRequest('/insights/procrastination-analysis', {
      method: 'GET'
    });
    return (response as any).data;
  }

  // 获取生产力建议
  async getProductivitySuggestions(): Promise<ProductivitySuggestions> {
    const response = await apiRequest('/insights/productivity-suggestions', {
      method: 'GET'
    });
    return (response as any).data;
  }

  // 获取任务预测分析
  async getTaskPredictions(): Promise<TaskPredictions> {
    const response = await apiRequest('/insights/task-predictions', {
      method: 'GET'
    });
    return (response as any).data;
  }
}

export const insightsAPI = new InsightsAPI();
export default insightsAPI;
